{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>电量趋势下降指标</th>\n",
       "      <th>线损指标</th>\n",
       "      <th>告警类指标</th>\n",
       "      <th>是否窃漏电</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>286</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>287</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>288</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>289</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>290</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>291 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     电量趋势下降指标  线损指标  告警类指标  是否窃漏电\n",
       "0           4     1      1      1\n",
       "1           4     0      4      1\n",
       "2           2     1      1      1\n",
       "3           9     0      0      0\n",
       "4           3     1      0      0\n",
       "..        ...   ...    ...    ...\n",
       "286         4     1      2      0\n",
       "287         1     0      2      0\n",
       "288         5     1      2      1\n",
       "289         2     1      0      0\n",
       "290         4     1      0      0\n",
       "\n",
       "[291 rows x 4 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from sklearn import tree\n",
    "from sklearn.datasets import load_wine\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "plt.rcParams['font.sans-serif']=['Microsoft Yahei']\n",
    "plt.rcParams['axes.unicode_minus']=False\n",
    "# np.random.randint(1,20,size=(100,3))\n",
    "data=pd.read_excel(\"./dataset.xls\")\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(203, 3)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Xtrain,Xtest,Ytrain,Ytest = train_test_split(data.iloc[:,:-1],\n",
    "                                             data.iloc[:,-1],\n",
    "                                             test_size=0.3,\n",
    "                                             random_state=420)\n",
    "\n",
    "Xtrain.shape                                         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 生成分类树"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9090909090909091"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf = DecisionTreeClassifier(criterion='gini') #生成决策树分类器   entropy\n",
    "clf = clf.fit(Xtrain,Ytrain)\n",
    "\n",
    "clf.score(Xtest,Ytest) #得到准确率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 混淆"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[76,  4],\n",
       "       [ 4,  4]], dtype=int64)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn import metrics\n",
    "metrics.confusion_matrix(Ytest,clf.predict(Xtest))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 预测准确率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "metrics.precision_score(Ytest,clf.predict(Xtest))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ROC曲线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY4AAAEKCAYAAAAFJbKyAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAn50lEQVR4nO3deZBc5Xnv8e+DhHahdYQEaGW0ThtjI2JHFmU2i8VgCItmUiaxjRMSnPjecpzNJnEqxLFzU/ZNbt0K8eUmuThOKjMgYWFsVoUlRiE2wjbQo31hkIQEg0AsQhJo5rl/vH2gaWY7M32W7vl9qqZmejvnaS39m/O+5zyvuTsiIiIDdULWBYiISG1RcIiISCwKDhERiUXBISIisSg4REQklpFZF5C06dOn+7x587IuQ0Skpjz55JMvuXtDT4/VfXDMmzePjRs3Zl2GiEhNMbOO3h7TUJWIiMSi4BARkVgUHCIiEouCQ0REYlFwiIhILAoOERGJRcEhIiKxKDjybtMmeOihrKsQEXlH3V8AWPO+8hV4/HF48cWsKxERAXTEkX/PPAOdnQoOEckNBUeevfEG7N4dfi4Ws61FRKREwZFnmza9+7OCQ0RyQsGRZ1FYjBih4BCR3NDkeJ61t8OYMbB8efhZRCQHdMSRZ8UiLFsGH/xg+Nk964pERBQcuVYsQlNT+HrtNdi7N+uKREQUHLn18svw/PNQKIQv0DyHiOSCgiOvojmNQiEccYCCQ0RyQcGRV+XBMXUqnHKKJshFJBcUHHlVLMLEiTB7drhdKOiIQ0RyQcGRV9HEuFm43dQULgjs6sq2LhEZ9lIJDjNbbWa7zWyHmV1f8dilZvaMme00s5vL7v+emW0tve7XBrKtuuEegiOaFIfw85Ej77YgERHJSOIXAJrZRODbwEeBLuAXZna3u3ea2STgVuBjwAHgYTN70N1/DPyRuz9vZkuAx4Dv9bWtpN9H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EouCQyQHzOwRM1tZhe2ca2brq1GTSG8UHCI1zsx+s3RVsEgqFBwite/TpN87TIYxBYdID8xsXmlVvb83s/1m9h0z+0yphfkmMzvdzFrMbIuZdZjZ35Re92dm9r9LP3/WzL7Xy/bHm9m/lvbxA0LjyOixT5jZLyys+vgXZdv6NzP7UWk1t38xsxNL+/oI0GpmN5U2cWLp8b1mdkeW7dalPik4RHo3D7gVWAhcCaxw98WEzqs3ENbD+CChK+21ZjaPsMjURWY2n7Cw0R/0su2vAK+7++nAHwFLAcxsKvB1wipySwmN+z5ces0FwG8DCwht1T/t7l8EfgK0uPtflp53NvAXQCNhRbyPDe2PQeS9FBwivdvn7j8vLf35DHBn6f6fEtpT7wO+TFi8aAJwqrsfAb4GPERoPHigl21fRFjPA3ffTFjyE8KqfosJ3W+LhPBaUHpsvbvvcffjQBthxbme/Mzdt7r7UULH3Nlx37hIXxQcIr0rX0GuC4iWRD1O6Ei7AdhFCI92IBoSOkYIkr7WdR5T8fjo0veRwL+X1nRY4u6nRcsHVzx/LL0vjXq07Oe3gRF91CESm4JDZHAmAxOBHxBC4AyA0loQ3yQMNX3JzGb08vqfUGq7XVp35YOl+38KfNzMGkuPnVv2mvPMbFqpbfevA9Fpt0eA6ZrLkLRoPQ6RwXmJsOLdbsKRR7F0/9eB29293cz+HvgfwOd6eP2fAv9mZh3Aj4H/BHD3583sS8CDZtZNGMJ6pPSap4E1hOGrf3X3B0r3f4+w3vYiQvCIJErrcYjUADP7LLDS3X8j61pENFQlIiKxaKhKJGFm9gjhLKzI7e7+1YzKERkyDVWJiEgsGqoSEZFYFBwiIhKLgkNERGJRcIiISCwKDhERieX/AxAt4CrpGCLAAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "test= []\n",
    "\n",
    "for i in range(10):\n",
    "    clf = tree.DecisionTreeClassifier(criterion='gini'\n",
    "                                 ,max_depth=i+1  #最大深度\n",
    "                                 #,min_samples_leaf=5 #子节点包含样本最小个数（父节点）\n",
    "                                  #,min_samples_split=20\n",
    "                                 ,random_state=30\n",
    "                                 ,splitter='random'\n",
    "                                 ) #生成决策树分类器   entropy\n",
    "\n",
    "    clf = clf.fit(Xtrain,Ytrain)\n",
    "    score = clf.score(Xtest,Ytest)\n",
    "    test.append(score)\n",
    "\n",
    "plt.plot(range(1,11),test,color='red')\n",
    "plt.ylabel('score')\n",
    "plt.xlabel('max_depth')\n",
    "plt.xticks(range(1,11))\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 画出树图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.38.0 (20140413.2041)\n -->\n<!-- Title: Tree Pages: 1 -->\n<svg width=\"465pt\" height=\"314pt\"\n viewBox=\"0.00 0.00 465.00 314.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 310)\">\n<title>Tree</title>\n<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-310 461,-310 461,4 -4,4\"/>\n<!-- 0 -->\n<g id=\"node1\" class=\"node\"><title>0</title>\n<polygon fill=\"#ea995e\" stroke=\"black\" points=\"285,-306 169,-306 169,-223 285,-223 285,-306\"/>\n<text text-anchor=\"middle\" x=\"227\" y=\"-290.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">线损指标 &lt;= 0.287</text>\n<text text-anchor=\"middle\" x=\"227\" y=\"-275.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">gini = 0.266</text>\n<text text-anchor=\"middle\" x=\"227\" y=\"-260.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">samples = 203</text>\n<text text-anchor=\"middle\" x=\"227\" y=\"-245.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">value = [171, 32]</text>\n<text text-anchor=\"middle\" x=\"227\" y=\"-230.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">class = 偷电</text>\n</g>\n<!-- 1 -->\n<g id=\"node2\" class=\"node\"><title>1</title>\n<polygon fill=\"#e68641\" stroke=\"black\" points=\"218,-187 90,-187 90,-104 218,-104 218,-187\"/>\n<text text-anchor=\"middle\" x=\"154\" y=\"-171.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">告警类指标 &lt;= 2.011</text>\n<text text-anchor=\"middle\" x=\"154\" y=\"-156.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">gini = 0.077</text>\n<text text-anchor=\"middle\" x=\"154\" y=\"-141.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">samples = 100</text>\n<text text-anchor=\"middle\" x=\"154\" y=\"-126.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">value = [96, 4]</text>\n<text text-anchor=\"middle\" x=\"154\" y=\"-111.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">class = 偷电</text>\n</g>\n<!-- 0&#45;&gt;1 -->\n<g id=\"edge1\" class=\"edge\"><title>0&#45;&gt;1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M201.673,-222.907C196.181,-214.105 190.315,-204.703 184.643,-195.612\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"187.545,-193.652 179.282,-187.021 181.606,-197.358 187.545,-193.652\"/>\n<text text-anchor=\"middle\" x=\"173.642\" y=\"-207.676\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">True</text>\n</g>\n<!-- 4 -->\n<g id=\"node5\" class=\"node\"><title>4</title>\n<polygon fill=\"#efb083\" stroke=\"black\" points=\"364,-187 236,-187 236,-104 364,-104 364,-187\"/>\n<text text-anchor=\"middle\" x=\"300\" y=\"-171.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">告警类指标 &lt;= 3.742</text>\n<text text-anchor=\"middle\" x=\"300\" y=\"-156.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">gini = 0.396</text>\n<text text-anchor=\"middle\" x=\"300\" y=\"-141.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">samples = 103</text>\n<text text-anchor=\"middle\" x=\"300\" y=\"-126.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">value = [75, 28]</text>\n<text text-anchor=\"middle\" x=\"300\" y=\"-111.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">class = 偷电</text>\n</g>\n<!-- 0&#45;&gt;4 -->\n<g id=\"edge4\" class=\"edge\"><title>0&#45;&gt;4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M252.327,-222.907C257.819,-214.105 263.685,-204.703 269.357,-195.612\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"272.394,-197.358 274.718,-187.021 266.455,-193.652 272.394,-197.358\"/>\n<text text-anchor=\"middle\" x=\"280.358\" y=\"-207.676\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">False</text>\n</g>\n<!-- 2 -->\n<g id=\"node3\" class=\"node\"><title>2</title>\n<polygon fill=\"#e5823b\" stroke=\"black\" points=\"98,-68 0,-68 0,-0 98,-0 98,-68\"/>\n<text text-anchor=\"middle\" x=\"49\" y=\"-52.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">gini = 0.021</text>\n<text text-anchor=\"middle\" x=\"49\" y=\"-37.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">samples = 93</text>\n<text text-anchor=\"middle\" x=\"49\" y=\"-22.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">value = [92, 1]</text>\n<text text-anchor=\"middle\" x=\"49\" y=\"-7.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">class = 偷电</text>\n</g>\n<!-- 1&#45;&gt;2 -->\n<g id=\"edge2\" class=\"edge\"><title>1&#45;&gt;2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M114.902,-103.726C106.068,-94.5142 96.6925,-84.7364 87.8685,-75.5343\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"90.3786,-73.095 80.9311,-68.2996 85.3261,-77.9399 90.3786,-73.095\"/>\n</g>\n<!-- 3 -->\n<g id=\"node4\" class=\"node\"><title>3</title>\n<polygon fill=\"#f8e0ce\" stroke=\"black\" points=\"207.5,-68 116.5,-68 116.5,-0 207.5,-0 207.5,-68\"/>\n<text text-anchor=\"middle\" x=\"162\" y=\"-52.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">gini = 0.49</text>\n<text text-anchor=\"middle\" x=\"162\" y=\"-37.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">samples = 7</text>\n<text text-anchor=\"middle\" x=\"162\" y=\"-22.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">value = [4, 3]</text>\n<text text-anchor=\"middle\" x=\"162\" y=\"-7.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">class = 偷电</text>\n</g>\n<!-- 1&#45;&gt;3 -->\n<g id=\"edge3\" class=\"edge\"><title>1&#45;&gt;3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M156.979,-103.726C157.585,-95.4263 158.225,-86.6671 158.838,-78.2834\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"162.329,-78.5281 159.567,-68.2996 155.348,-78.018 162.329,-78.5281\"/>\n</g>\n<!-- 5 -->\n<g id=\"node6\" class=\"node\"><title>5</title>\n<polygon fill=\"#eda876\" stroke=\"black\" points=\"345,-68 241,-68 241,-0 345,-0 345,-68\"/>\n<text text-anchor=\"middle\" x=\"293\" y=\"-52.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">gini = 0.359</text>\n<text text-anchor=\"middle\" x=\"293\" y=\"-37.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">samples = 98</text>\n<text text-anchor=\"middle\" x=\"293\" y=\"-22.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">value = [75, 23]</text>\n<text text-anchor=\"middle\" x=\"293\" y=\"-7.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">class = 偷电</text>\n</g>\n<!-- 4&#45;&gt;5 -->\n<g id=\"edge5\" class=\"edge\"><title>4&#45;&gt;5</title>\n<path fill=\"none\" stroke=\"black\" d=\"M297.393,-103.726C296.863,-95.4263 296.303,-86.6671 295.767,-78.2834\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"299.26,-78.0559 295.129,-68.2996 292.274,-78.5025 299.26,-78.0559\"/>\n</g>\n<!-- 6 -->\n<g id=\"node7\" class=\"node\"><title>6</title>\n<polygon fill=\"#399de5\" stroke=\"black\" points=\"457,-68 363,-68 363,-0 457,-0 457,-68\"/>\n<text text-anchor=\"middle\" x=\"410\" y=\"-52.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">gini = 0.0</text>\n<text text-anchor=\"middle\" x=\"410\" y=\"-37.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">samples = 5</text>\n<text text-anchor=\"middle\" x=\"410\" y=\"-22.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">value = [0, 5]</text>\n<text text-anchor=\"middle\" x=\"410\" y=\"-7.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">class = 不偷电</text>\n</g>\n<!-- 4&#45;&gt;6 -->\n<g id=\"edge6\" class=\"edge\"><title>4&#45;&gt;6</title>\n<path fill=\"none\" stroke=\"black\" d=\"M340.96,-103.726C350.214,-94.5142 360.036,-84.7364 369.281,-75.5343\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"371.93,-77.8351 376.548,-68.2996 366.992,-72.8741 371.93,-77.8351\"/>\n</g>\n</g>\n</svg>\n",
      "text/plain": [
       "<graphviz.sources.Source at 0x247e2c34e20>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn import tree\n",
    "import graphviz  # pip install graphviz\n",
    "graphviz.__version__\n",
    "\n",
    "clf = tree.DecisionTreeClassifier(criterion='gini'\n",
    "                                 ,max_depth=2  #最大深度\n",
    "                                 #,min_samples_leaf=5 #子节点包含样本最小个数（父节点）\n",
    "                                  #,min_samples_split=20\n",
    "                                 ,random_state=30\n",
    "                                 ,splitter='random'\n",
    "                                 ) #生成决策树分类器   entropy\n",
    "\n",
    "clf = clf.fit(Xtrain,Ytrain)\n",
    "dot_data = tree.export_graphviz(clf\n",
    "                     ,feature_names = [\"电量趋势下降指标\",\"线损指标\",\"告警类指标\"] #Xtrain 特征列\n",
    "                     ,class_names = [\"偷电\",\"不偷电\"] #y标签\n",
    "                     ,filled=True #渲染颜色\n",
    "                    )\n",
    "graph = graphviz.Source(dot_data,filename='决策树.pdf')\n",
    "graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "score = clf.score(Xtest,Ytest)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "7f57c1fbf45e57d6af804ad8e48983c17b1e27df09c4020de9dad2e347f594a9"
  },
  "kernelspec": {
   "display_name": "Python 3.10.0 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
